package com.core.convenientCode.common;

import com.common.utils.StringUtils;
import com.model.pojo.sys.SysGenTable;
import com.model.pojo.sys.SysGenTableColumn;
import org.apache.commons.lang3.RegExUtils;

import java.util.Arrays;

/**
 * @Author: zql
 * @Description:
 * @Data: 14:53 2021/6/1
 */
public class GenUtils {

    /**
     * 初始化表信息
     */
    public static void initTable(SysGenTable genTable, String operName)
    {
        String packageName = "com";
        //类名
        genTable.setClassName(convertClassName(genTable.getTableName()));
        //生成包路径
        genTable.setPackageName(packageName);
        genTable.setModuleName(getModuleName(""));
        genTable.setBusinessName(getBusinessName(genTable.getTableName()));
        genTable.setFunctionName(replaceText(genTable.getTableComment()));
        genTable.setFunctionAuthor("zql");
        genTable.setCreator(operName);
    }

    /**
     * 表名转换成Java类名
     *
     * @param tableName 表名称
     * @return 类名
     */
    private static String convertClassName(String tableName)
    {
        boolean autoRemovePre = false;
        String tablePrefix ="";
        if (autoRemovePre && StringUtils.isNotEmpty(tablePrefix))
        {
            String[] searchList = StringUtils.split(tablePrefix, ",");
            tableName = replaceFirst(tableName, searchList);
        }
        return StringUtils.convertToCamelCase(tableName);
    }

    /**
     * 批量替换前缀
     *
     * @param replacementm 替换值
     * @param searchList 替换列表
     * @return
     */
    public static String replaceFirst(String replacementm, String[] searchList)
    {
        String text = replacementm;
        for (String searchString : searchList)
        {
            if (replacementm.startsWith(searchString))
            {
                text = replacementm.replaceFirst(searchString, "");
                break;
            }
        }
        return text;
    }

    /**
     * 获取模块名
     *
     * @param packageName 包名
     * @return 模块名
     */
    public static String getModuleName(String packageName)
    {
        int lastIndex = packageName.lastIndexOf(".");
        int nameLength = packageName.length();
        String moduleName = StringUtils.substring(packageName, lastIndex + 1, nameLength);
        return moduleName;
    }

    /**
     * 获取业务名
     *
     * @param tableName 表名
     * @return 业务名
     */
    public static String getBusinessName(String tableName)
    {
        int lastIndex = tableName.lastIndexOf("_");
        int nameLength = tableName.length();
        String businessName = StringUtils.substring(tableName, lastIndex + 1, nameLength);
        return businessName;
    }

    /**
     * 关键字替换
     *
     * @param text 需要被替换的名字
     * @return 替换后的名字
     */
    public static String replaceText(String text)
    {
        return RegExUtils.replaceAll(text, "(?:表)", "");
    }

    /**
     * 初始化列属性字段
     */
    public static void initColumnField(SysGenTableColumn column, SysGenTable table)
    {
        String dataType = getDbType(column.getColumnType());
        //字段名
        String columnName = column.getColumnName();
        column.setSysGenTableId(table.getSysGenTableId());
        column.setCreator(table.getCreator());

        // 设置java字段名
        column.setJavaField(StringUtils.toCamelCase(columnName));
        // 类型
        ColumnType columnType = ColumnType.getType(column.getColumnType());
        column.setJavaType(columnType.getJavaType());

        column.setJdbcType(columnType.getJdbcType());
        column.setMinSize(0);
        if(ColumnType.TINYINT.getJavaType().equals(columnType.getJavaType())){
            //Byte类型就进入该方法，byte类型的大小
            Integer columnLength =  getByteColumnLength(column.getColumnComment());
            column.setMaxSize(columnLength);

        }else if(ColumnType.VARCHAR.arraysContains(dataType)){
            //设定字符串最大最小值
            Integer columnLength = getColumnLength(column.getColumnType());
            column.setMaxSize(columnLength);
        }
        else if(ColumnType.LONGVARCHAR.arraysContains(dataType)){
            //文本类型
            column.setMaxSize(10240);
        }
        // 插入字段（默认所有字段都需要插入）
        column.setIsInsert((byte) 1);
    }


    /**
     * 获取数据库类型字段
     *
     * @param columnType 列类型
     * @return 截取后的列类型
     */
    public static String getDbType(String columnType)
    {
        if (StringUtils.indexOf(columnType, "(") > 0)
        {
            return StringUtils.substringBefore(columnType, "(");
        }
        else
        {
            return columnType;
        }
    }

    /**
     * 校验数组是否包含指定值
     *
     * @param arr 数组
     * @param targetValue 值
     * @return 是否包含
     */
    public static boolean arraysContains(String[] arr, String targetValue)
    {
        return Arrays.asList(arr).contains(targetValue);
    }


    /**
     * 获取字段长度
     *
     * @param columnType 列类型
     * @return 截取后的列类型
     */
    public static Integer getColumnLength(String columnType)
    {
        if (StringUtils.indexOf(columnType, "(") > 0)
        {
            String length = StringUtils.substringBetween(columnType, "(", ")");
            return Integer.valueOf(length);
        }
        else
        {
            return 0;
        }
    }
    /**
     * @Author: zql
     * @Description:  返回自定义枚举类型最大值  根据枚举类型备注定义 示例： 1:枚举A,2:枚举B
     * @param getColumnComment
     * @Data: 10:36 2021/6/8
     */
    public static Integer getByteColumnLength(String getColumnComment){
        if (StringUtils.indexOf(getColumnComment, ",") > 0){
            int startIndex = getColumnComment.lastIndexOf(",");
            int endIndex = getColumnComment.lastIndexOf(":");
            String length =  StringUtils.substring(getColumnComment, startIndex+1,endIndex);
            return Integer.valueOf(length);
        }
        return 0;
    }
}
